﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using WebUI.Helper;
using WebUI.Models;

namespace WebUI.Controllers
{
    public class FolderController : Controller
    {
        string root = "../upload";
        private static JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
        //
        // GET: /Folder/
        [AjaxAuthorize(Roles = "普通用户,系统管理员")]
        public JsonResult LoadAll()
        {
            bool success = false;
            string Msg = "";
            int total = 0;
            List<Object> list = null;
            try
            {
                string path = Request["node"] ?? "/";
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath(root + path));
                list = new List<Object>(dir.GetDirectories().Length);

                foreach (var c in dir.GetDirectories())
                {
                    list.Add(new
                    {
                        id = path + c.Name + "/",
                        text = c.Name,
                        parentId = path
                    });
                    total++;
                }
                success = true;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
            }
            return Json(new { success = success, total = total, Msg = Msg, data = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <returns></returns>
        [AjaxAuthorize(Roles = "普通用户,系统管理员")]
        public JsonResult Create()
        {
            bool success = false;
            string msg = "";
            Dictionary<string, Object> obj = null;
            string data = Request["data"] ?? "";
            if (string.IsNullOrEmpty(data))
            {
                msg = "发送的数据错误!";
            }
            else
            {
                obj = javascriptSerializer.Deserialize<Dictionary<string, Object>>(data);

                if (obj.Count() > 0)
                {
                    string partdir = (string)obj["parentId"];
                    string foldername = (string)obj["text"];

                    try
                    {
                        string dirPath = Server.MapPath(root + partdir);
                        if (Directory.Exists(dirPath + foldername))
                        {
                            msg = "目录已经存在!";
                        }
                        else
                        {
                            DirectoryInfo di = Directory.CreateDirectory(dirPath + foldername);
                            obj["id"] = partdir + foldername + "/";
                            success = true;
                        }
                    }
                    catch (Exception e)
                    {
                        msg = e.Message;
                    }
                }
                else
                {
                    msg = "发送的数据错误";
                }
            }

            return Json(new { success = success, total = 0, Msg = msg, data = obj });
        }

        [AjaxAuthorize(Roles = "普通用户,系统管理员")]
        public JsonResult Update()
        {
            bool success = false;
            string msg = "";
            Dictionary<string, object> obj = null;
            string data = Request["data"] ?? "";
            if (string.IsNullOrEmpty(data))
                msg = "传递的数据格式错误!";
            else
            {
                try
                {
                    obj = javascriptSerializer.Deserialize<Dictionary<string, object>>(data);
                    if (obj.Count() > 0)
                    {
                        string parentDir = (string)obj["parentId"]; //父级目录
                        string folderName = (string)obj["text"]; //目录名称
                        string dirPath = Server.MapPath(root + parentDir);//实际目录
                        string oldPath = Server.MapPath(root + (string)obj["id"]);//原来的目录
                        if (Directory.Exists(oldPath))
                        {
                            if (Directory.Exists(dirPath + folderName))
                            {
                                msg = "目录已经存在!";
                            }
                            else
                            {
                                Directory.Move(oldPath, dirPath + folderName);
                                obj["id"] = parentDir + folderName + "/";
                                success = true;
                            }
                        }
                        else
                        {
                            msg = "修改的目录不存在!";
                        }
                    }
                    else
                    {
                        msg = "传递的数据格式错误!";
                    }
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            return Json(new { success = success, total = 0, Msg = msg, data = obj });
        }


        [AjaxAuthorize(Roles = "普通用户,系统管理员")]
        public JsonResult Remove()
        {
            bool success = false;
            string msg = "";
            Dictionary<string, Object> obj = null;
            string data = Request["data"] ?? "";
            if (string.IsNullOrEmpty(data))
            {
                msg = "错误的传输数据!";
            }
            else
            {
                try
                {
                    obj = javascriptSerializer.Deserialize<Dictionary<string, Object>>(data);
                    if (obj.Count() > 0)
                    {
                        string parentDir = (string)obj["parentid"];
                        string foldername = (string)obj["text"];
                        string dirPath = Server.MapPath(root + parentDir);
                        if (Directory.Exists(dirPath + foldername))
                        {
                            Directory.Delete(dirPath + foldername, true);
                            success = true;
                        }
                        else
                        {
                            msg = "目录已被删除!";
                        }
                    }
                    else
                    {
                        msg = "错误的传输数据!";
                    }
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            return Json(new { success = success, total = 0, Msg = msg, data = obj });
        }
    }
}
